function EditVehicleFillUpView() {
	var self = Ti.UI.createView();
	
	// Data for the rows in our form
	var data = [];
	
	var headerRow = Ti.UI.createTableViewRow({
		height:50,
		selectionStyle:Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE
	});
	var l = Ti.UI.createLabel({
		text:'Edit Existing Fill-Up',
		color:'#999',
		textAlign:'center'
	});
	headerRow.add(l);
	data.push(headerRow);
	
	//  Odometer input
	var inputOdometerRow = Ti.UI.createTableViewRow({height:50});
	var inputOdometerLabel = Ti.UI.createLabel({
		text:'odometer',
		font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'},
		height:'auto',
		width:'auto',
		left:10
	});
	var inputOdometerField = Titanium.UI.createTextField({
		color:'#336699',
		height:35,
		right:65,
		width:90,
		textAlign:'right',
		font:{fontSize:14,fontFamily:'Arial'},
		keyboardType:Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION,
		returnKeyType:Titanium.UI.RETURNKEY_DEFAULT
	});
	var inputDistanceType = Ti.UI.createLabel({
		text:'km',
		font:{fontSize:14,fontFamily:'Arial'},
		color:'#336699',
		height:35,
		right:20
	});
	inputOdometerRow.add(inputOdometerLabel);
	inputOdometerRow.add(inputOdometerField);
	inputOdometerRow.add(inputDistanceType);
	inputOdometerRow.addEventListener('click', function(e) {
		inputOdometerField.focus();
	});
	data.push(inputOdometerRow);
	
	//  Quantity input
	var inputQuantityRow = Ti.UI.createTableViewRow({height:50});
	var inputQuantityLabel = Ti.UI.createLabel({
		text:'quantity',
		font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'},
		height:'auto',
		width:'auto',
		left:10
	});
	var inputQuantityValue = Titanium.UI.createTextField({
		color:'#336699',
		height:35,
		right:65,
		width:90,
		textAlign:'right',
		font:{fontSize:14,fontFamily:'Arial'},
		keyboardType:Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION,
		returnKeyType:Titanium.UI.RETURNKEY_DEFAULT
	});
	var inputQuantityType = Ti.UI.createLabel({
		text:'liters',
		font:{fontSize:14,fontFamily:'Arial'},
		color:'#336699',
		height:35,
		right:20
	});
	inputQuantityRow.addEventListener('click', function(e) {
		inputQuantityValue.focus();
	});
	inputQuantityRow.add(inputQuantityLabel);
	inputQuantityRow.add(inputQuantityValue);
	inputQuantityRow.add(inputQuantityType);
	data.push(inputQuantityRow);
	
	// Price per unit of quantity
	var inputPriceRow = Ti.UI.createTableViewRow({height:50});
	var inputPriceLabel = Ti.UI.createLabel({
		text:'price per liter',
		font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'},
		height:'auto',
		width:'auto',
		left:10
	});
	var inputPriceValue = Titanium.UI.createTextField({
		color:'#336699',
		height:35,
		right:65,
		width:90,
		textAlign:'right',
		font:{fontSize:14,fontFamily:'Arial'},
		keyboardType:Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION,
		returnKeyType:Titanium.UI.RETURNKEY_DEFAULT
	});
	
	var Conversions = require('tools/Conversions');
	
	var inputCurrencyType = Ti.UI.createLabel({
		text:Conversions.getCurrentCurrency(),
		font:{fontSize:14,fontFamily:'Arial'},
		color:'#336699',
		height:35,
		right:20
	});
	inputPriceRow.addEventListener('click', function(e) {
		inputPriceValue.focus();
	});
	inputPriceRow.add(inputPriceLabel);
	inputPriceRow.add(inputPriceValue);
	inputPriceRow.add(inputCurrencyType);
	data.push(inputPriceRow);

	// Fillup date
	var inputDateRow = Ti.UI.createTableViewRow({height:50});
	var inputDateLabel = Ti.UI.createLabel({
		text:'date',
		font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'},
		height:'auto',
		width:'auto',
		left:10
	});
	var todaysDate = new Date();
	var inputDateValue = Ti.UI.createLabel({
		text:String.formatDate(todaysDate) + ' ' + String.formatTime(todaysDate),
		font:{fontSize:12,fontFamily:'Arial'},
		right:65
	});
	var inputDateButton = Ti.UI.createButton({
		style:Titanium.UI.iPhone.SystemButton.DISCLOSURE,
		right:10
	});
	inputDateRow.add(inputDateLabel);
	inputDateRow.add(inputDateValue);
	inputDateRow.add(inputDateButton);
	inputDateRow.addEventListener('click', function(e) {
		self.fireEvent('showDatePickerEvent', e);
	});
	data.push(inputDateRow);
	
	var inputPartialSettingRow = Ti.UI.createTableViewRow({height:50});
	var inputPartialSettingLabel = Ti.UI.createLabel({
		text:'is this a partial fillup?',
		font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'},
		height:'auto',
		width:'auto',
		left:10
	});
	var inputPartialSettingSwitch = Titanium.UI.createSwitch({
		value:false,
		right:10,
		enabled:false
	});
	inputPartialSettingRow.add(inputPartialSettingLabel);
	inputPartialSettingRow.add(inputPartialSettingLabel);
	inputPartialSettingRow.add(inputPartialSettingSwitch);
	data.push(inputPartialSettingRow);
	
	
	// Create table view
	var tableView = Ti.UI.createTableView({
		data:data,
		style: Titanium.UI.iPhone.TableViewStyle.GROUPED
	});

	self.add(tableView);
	
	self.addEventListener('setFillUpDateEvent', function(e) {
		inputDateValue.setText(e.pickedDateTime);
	});
	
	self.addEventListener('resetEvent', function(e) {
		currentVehicleId = e.vehicleId;
		currentFillupId = e.fillupId;
		resetAll(e.fillupId, inputOdometerField, inputQuantityValue, inputPriceValue, inputDateValue, 
			inputPartialSettingSwitch);
	});
	
	self.addEventListener('wantToUpdateVehicleFillUpEvent', function(e) {
		
		// We got a message to save this fillup. First we do some rectifying
		var curOdometerValue = inputOdometerField.getValue();
		if(curOdometerValue) {
			var rectifiedOdometer = curOdometerValue.replace(/\s/g,'');
			inputOdometerField.setValue(rectifiedOdometer);
		}
		
		// Then validation
		var validationError = validateFillUpData(
			inputOdometerField.getValue(),
			inputQuantityValue.getValue(),
			inputPriceValue.getValue()
		);
		if(validationError) {
			Ti.UI.createAlertDialog({
    			message: validationError,
    			ok: 'Ok',
    			title: 'Sorry...'
    		}).show();
		} else {
			// We invoke a updateVehicleFillUpEvent, passing along all the useful info
			// the controller needs to save the fillup
			var updatedFillupData = {
				vehicleId: currentVehicleId,
				fillupId: currentFillupId,
				odometer: inputOdometerField.getValue(),
				quantity: inputQuantityValue.getValue(),
				price: inputPriceValue.getValue(),
				date: inputDateValue.getText()
			};
			
			// For now we don't allow partial to be edited
			// updatedFillupData.isPartial = inputPartialSettingSwitch.getValue();
			self.fireEvent('updateVehicleFillUpEvent', updatedFillupData);
		}
	});
	
	return self;
}

var currentVehicleId = null;
var currentFillupId = null;

function resetAll(fillupId, inputOdometerField, inputQuantityValue, inputPriceValue, inputDateValue, 
	              inputPartialSettingSwitch) {
	var VehicleFillupModel = require('models/VehicleFillup');
	
	var fillup = VehicleFillupModel.findByFillupID(fillupId);
	if(fillup == null) {
		return;
	}
	
	inputOdometerField.setValue(String(fillup.vehicleOdometer));
	inputQuantityValue.setValue(String(fillup.fillupUnits));
	inputPriceValue.setValue(String(fillup.pricePerUnit));
	var actualDate = fillup.fillupDate; 
	inputDateValue.setText(String.formatDate(actualDate) + ' ' + String.formatTime(actualDate));
	inputPartialSettingSwitch.setValue(fillup.isPartial);
}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function validateFillUpData(odometer, quantity, price) {
	var errorMessage;
	
	if(!isNumber(odometer) || odometer <= 0) {
		errorMessage = 'Odometer needs to be a valid integer (No decimals allowed).';
	}
	else if(!isNumber(quantity) || quantity <= 0) {
		errorMessage = 'Quantity needs to be a valid number';
	}
	else if(!isNumber(price) || price <= 0) {
		errorMessage = 'Price needs to be a positive number';
	}
	return errorMessage;
}

module.exports = EditVehicleFillUpView;